package zhy.use_view_scroll_and_scroller;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class UseViewScroll2Activity extends Activity {
	static View view;
	Button up,down;
	static Scroller scroller;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.use_view_scroll2);
        scroller=new Scroller(this);
        view=findViewById(R.id.view);
        
        OnClickListener listener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.up:
					//view.scrollBy(0, 25);
					scroller.startScroll(view.getScrollX(), view.getScrollY(), 0, 25, 3000);
					view.invalidate();
					break;
				case R.id.down:
					//view.scrollBy(0, -50);
					scroller.startScroll(view.getScrollX(), view.getScrollY(), 0, -50,8000);
					view.invalidate();
					break;

				default:
					break;
				}
			}
		};
        up=(Button)findViewById(R.id.up);
        up.setOnClickListener(listener);
        down=(Button)findViewById(R.id.down);
        down.setOnClickListener(listener);
    }
	
	public static class MyLinearLayout extends LinearLayout
	{
		public MyLinearLayout(Context context) {
			super(context);
		}

		public MyLinearLayout(Context context, AttributeSet attrs) {
			super(context, attrs);
		}
		
		@Override
		public void computeScroll() {
			if(scroller.computeScrollOffset())
			{
				view.scrollTo(scroller.getCurrX(),scroller.getCurrY());
				//postInvalidate();
				invalidate();
			}
		}
		
		
	}
}